using System;
using UnityEngine;

public class TableTennisCoinController : MonoBehaviour
{
	public UILabel coins;

	public CurrencyType currency;

	public UISprite currencySprite;

	public UISprite fillSprite;

	protected bool canUpdate = true;

	public PanelLayerControl panelControl;

	public bool showMax
	{
		get
		{
			WalletManager walletManager = GGPlayerSettings.instance.walletManager;
			return fillSprite != null && walletManager.CurrencyHasMax(currency);
		}
	}

	public string displayFormat
	{
		get
		{
			WalletManager walletManager = GGPlayerSettings.instance.walletManager;
			return showMax ? ("{0}/" + GGFormat.FormatWinnings(walletManager.MaxCurrencyCount(currency))) : "{0}";
		}
	}

	private void OnEnable()
	{
		UpdateCoins();
	}

	private void Update()
	{
		if (canUpdate)
		{
			UpdateCoins();
		}
	}

	private void UpdateCoins()
	{
		WalletManager walletManager = GGPlayerSettings.instance.walletManager;
		UITools.ChangeText(coins, string.Format(displayFormat, walletManager.CurrencyCount(currency)));
		if (showMax)
		{
			fillSprite.fillAmount = (float)walletManager.CurrencyCount(currency) / (float)walletManager.MaxCurrencyCount(currency);
		}
	}

	public void OnClickToAddCoins()
	{
		TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
		tableTennisMainNavigation.PushCurrencyStore(currency);
	}

	public void AnimateSpendMoney(UIWidget visibleWidget, int amount, Action onComplete)
	{
		WalletManager walletManager = GGPlayerSettings.instance.walletManager;
		int prevAmount = walletManager.CurrencyCount(currency) + amount;
		int amount2 = walletManager.CurrencyCount(currency);
		AnimateChangeMoney(visibleWidget, prevAmount, amount2, onComplete);
	}

	public void AnimateGetMoney(UIWidget visibleWidget, int amount, Action onComplete)
	{
		WalletManager walletManager = GGPlayerSettings.instance.walletManager;
		int prevAmount = walletManager.CurrencyCount(currency) - amount;
		int amount2 = walletManager.CurrencyCount(currency);
		AnimateChangeMoney(visibleWidget, prevAmount, amount2, onComplete);
	}

	public void AnimateChangeMoney(UIWidget visibleWidget, int prevAmount, int amount, Action onComplete)
	{
		WalletManager walletManager = GGPlayerSettings.instance.walletManager;
		canUpdate = false;
		float fromValue = prevAmount;
		float num = amount;
		if (walletManager.CurrencyHasMax(currency))
		{
			fromValue = (float)prevAmount / (float)walletManager.MaxCurrencyCount(currency);
			num /= (float)walletManager.MaxCurrencyCount(currency);
		}
		panelControl.ShowParticlesOnFill(visibleWidget, currencySprite, fillSprite, coins, prevAmount, amount, displayFormat, fromValue, num, currencySprite.spriteName, 0.5f, delegate
		{
			canUpdate = true;
			if (onComplete != null)
			{
				onComplete();
			}
		});
	}
}
